<template>
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="90%">
    <el-form :model="form" ref="form" :rules="rules" size="small" label-width="150px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" clearable />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="dialog-footer" slot="footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {accAdd, addAction, getAction,deleteAction, updateAction, getById,accMul} from '@/api/manage'
export default {
  name: "EditGoodsForm",
  dicts: ['goods_status', 'isdo', 'goods_order_in'],
  data() {
    return {
      // 弹出层标题
      title: "编辑产品备注",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        /*goodsId: [{ required: true, message: "产品编码不能为空", trigger: "blur" }],
        gongchengName: [{ required: true, message: "工程名称不能为空", trigger: "blur" }],
        goodsCode: [{ required: true, message: "产品编码不能为空", trigger: "blur" }],
        goodsName: [{ required: true, message: "产品名称不能为空", trigger: "blur" }],
        goodModel: [{ required: true, message: "产品型号不能为空", trigger: "blur" }],
        status: [{ required: true, message: "产品状态不能为空", trigger: "blur" }],
        orderCount: [{ required: true, message: "订单数量不能为空", trigger: "blur" }],
        deliveryTime: [{ required: true, message: "需求日期不能为空", trigger: "blur" }],
        stockCount: [{ required: true, message: "库存总量不能为空", trigger: "blur" }],
        remainderCount: [{ required: true, message: "可用数量不能为空", trigger: "blur" }],
        lockCount: [{ required: true, message: "锁定数量不能为空", trigger: "blur" }],
        pleaseBuyCount: [{ required: true, message: "请购数量不能为空", trigger: "blur" }],
        unit: [{ required: true, message: "产品单位不能为空", trigger: "blur" }],
        goodsUnitPrice: [{ required: true, message: "销售单价不能为空", trigger: "blur" }],
        goodsTotalPrice: [{ required: true, message: "总价不能为空", trigger: "blur" }],
        goodsTotalTaxPrice: [{ required: true, message: "含税总额不能为空", trigger: "blur" }],
        fahuoShengyu: [{ required: true, message: "剩余发货不能为空", trigger: "blur" }],
        isdo: [{ required: true, message: "是否自制不能为空", trigger: "blur" }],*/
      },
      url: {
        editRemark: '/goodsSale/goodsSale/editRemark',
          info: '/goodsSale/goodsSale/',
          goodsOrderInUrl: '/goodsOrderIn/goodsOrderIn/listOptions',
          goodsOrderInById: '/goodsOrderIn/goodsOrderIn/',
      },
      goodsOrderInOptions:[],
    };
  },
  created() {
    this.getGoodsOrderInList();
  },
  methods: {
    getGoodsOrderInList() {
      let params = {};
      getAction(this.url.goodsOrderInUrl, params).then(response => {
        this.goodsOrderInOptions = response.rows;
      });
    },

    selectGoodsOrderIn(value) {
      let that = this;
      let arr = that.$refs.mySelectGoodsOrderIn;
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].currentValue == value) {
          that.form.goodsCode = arr[i].label;
          /*that.form.goodsName = arr[i].remark;
          that.form.goodModel = arr[i].$attrs.data.dictType;*/
          getById(this.url.goodsOrderInById, value).then(response => {
            //this.form = response.data;
            that.form.goodsName = response.data.name;
            that.form.goodModel = response.data.model;
          });
        }
      }
    },

    handleChangeMoneyEdit(row) {
      let that = this;
      let handleReckonMoneyEdit = (row) => {
        let row_money = 0;
        let row_tax_money = 0;
        row_money = accMul(Number(that.form.goodsUnitPrice), Number(that.form.orderCount))
        that.$set(that.form, 'goodsTotalPrice', row_money);
        row_money = accMul(Number(that.form.goodsUnitPrice), Number(1.13))
        that.$set(that.form, 'goodsTaxUnitPrice', row_money);
        row_tax_money = accMul(Number(that.form.goodsTaxUnitPrice), Number(that.form.orderCount))
        that.$set(that.form, 'goodsTaxTotalPrice', row_tax_money);
      };
      handleReckonMoneyEdit(row);
    },
    show(row) {
      if (row.id == undefined) {
        this.form = {};
      } else {
        getById(this.url.info , row.id).then(response => {
          this.form = response.data;
        });
      }
      this.open = true;
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateAction(this.url.editRemark, this.form).then(response => {
          this.$emit('ok', this.form);
            this.open = false;
          });
          // 这里可以添加提交逻辑
        }
      });
    },
    cancel() {
      this.open = false;
    },
  }
};
</script>
